package com.hengby.hengcrud.modular.job.controller;

import com.hengby.hengcrud.core.common.annotation.BussinessLog;
import com.hengby.hengcrud.core.common.annotation.Permission;
import com.hengby.hengcrud.core.common.dictmap.JobDict;
import com.hengby.hengcrud.core.exception.HengCrudException;
import com.hengby.hengcrud.core.exception.HengCrudExceptionEnum;
import com.hengby.hengcrud.core.util.ToolUtil;
import com.hengby.hengcrud.modular.base.controller.BaseController;
import com.hengby.hengcrud.modular.base.tips.Tips;
import com.hengby.hengcrud.modular.job.model.Job;
import com.hengby.hengcrud.modular.job.service.JobService;
import com.hengby.hengcrud.modular.job.wrapper.JobWrapper;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.quartz.CronExpression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @author zhouheng
 */
@Controller
@RequestMapping(value = "/job")
public class JobController extends BaseController {

    private static final String PREFIX_ = "/system/job/";

	@Autowired
	private JobService jobService;

    @RequestMapping("")
    public String job(){
        return PREFIX_+ "job";
    }

	@RequestMapping("job")
	public String index() {
		return PREFIX_ + "job";
	}

	@RequestMapping("/list")
    @RequiresPermissions("job:list")
	@ResponseBody
	public Object jobList(@RequestParam(required = false,defaultValue = "") String name,
                          @RequestParam(required = false,defaultValue = "") String methodName) {

        List<Map<String, Object>> jobList = this.jobService.list(name, methodName);
        Object object = new JobWrapper(jobList).wrap();

        return object;
	}

	@RequestMapping("/job_add")
    @RequiresPermissions("job:add")
    public String addJob(){
        return PREFIX_ + "job_add";
    }

    @RequestMapping("/add")
    @BussinessLog(value = "新增Job", key = "jobId", dict= JobDict.class)
    @ApiOperation(value = "新增Job信息")
    @ResponseBody
    public Tips add(@Valid Job job, BindingResult result){
        if (result.hasErrors()){
            throw new HengCrudException(HengCrudExceptionEnum.REQUEST_NULL);
        }

        try {
            this.jobService.addJob(job);
        } catch (Exception e) {
            e.printStackTrace();
            throw new HengCrudException(HengCrudExceptionEnum.JOB_ADD_ERROR);
        }

        return SUCCESS_TIP;
    }

    /**
     * 立即运行
     * @param jobIds
     * @return
     */
//    @BussinessLog(value = "立即运行定时任务", key = "", dict )
    @RequestMapping("/run")
    @ResponseBody
    public Tips run(@RequestParam String jobIds) {

        if (ToolUtil.isEmpty(jobIds)) {
            throw new HengCrudException(HengCrudExceptionEnum.REQUEST_NULL);
        }

        this.jobService.run(jobIds);

        return SUCCESS_TIP;
    }

	@RequestMapping("job/checkCron")
	@ResponseBody
	public boolean checkCron(String cron) {
		try {
			return CronExpression.isValidExpression(cron);
		} catch (Exception e) {
			return false;
		}
	}

    /**
     * 暂停任务
     * @param jobIds
     * @return
     */
    @RequestMapping("/pause")
    @ResponseBody
    public Tips pause(@RequestParam String jobIds) {

        if (ToolUtil.isEmpty(jobIds)) {
            throw new HengCrudException(HengCrudExceptionEnum.REQUEST_NULL);
        }

        this.jobService.pause(jobIds);

        return SUCCESS_TIP;
    }
}
